<?php
/**
 * BiBoB SMS Gateway Class
 * 
 * Class using the BiBoB Webservice to send out SMS messages.
 * Furthermore there is a few of utilities functions that can be used too.
 * 
 * If you have any questions - feel free to contact me.
 * 
 * Developed by:
 * Johan Holst Nielsen
 * johan@phpgeek.dk
 * www.phpgeek.dk
 * 
 * Use it as you want - and distribute is as you want - but please leve this message.
 *
 * Read more about BiBoB at www.bibob.dk
 * 
 * EXAMPLES
 * 
 * Send out an SMS:
 * <?php
 * $sms = new BiBoB_SMS('YourBiBoBNumber','YourBiBoBPassword');
 * if($sms->Login()===true) {
 *   $result = $sms->SendSms('YourFriendsNumber','A nice and friendly message');
 *      if($result === true) {
 *         echo 'Message is sent';
 *      }
 * }
 * ?>
 * 
 * Get your account Balance:
 * <?php
 * $sms = new BiBoB_SMS('YourBiBoBNumber','YourBiBoBPassword');
 * if($sms->Login()===true) {
 *   $balance = $sms->GetBalance();
 *   echo 'I got '.sprintf('%.2f',$balance).' DKK left on my account';
 * }
 * ?>
 * 
 * Please read the comments in the source for more information.
 * 
 */
class BiBoB_SMS {
    
    /**
     * Defines where the SOAP Client can find the BiBoB SMS WSDL
     *
     * @var string
     */
    private $wsdl = 'https://www.bibob.dk/SmsSender.asmx?WSDL';
    
    /**
     * Variable for the cellphone number. If you set your cellphone number here - you
     * do not need to add it when initialize the class.
     *
     * @var string
     */
    protected $cellphone = '';
    
    /**
     * Varibale for the password in plain text. If you set your password here - you
     * do not need to add it when initialize the class.
     *
     * @var string
     */
    protected $password = '';
    
    
    /**
     * Contructs the class - and adds cellphone number and password
     * 
     * Set the cellphone number and password. 
     *
     * @param string $cellphone
     * @param string $password
     */
    public function __construct($cellphone='',$password='') {
        if(!empty($cellphone)) { $this->cellphone = $cellphone; }
        if(!empty($password)) {  $this->password = md5($password); }
    }
    
    
    /**
     * Performs a login
     * 
     * Function is not needed to do anything. But use it to be sure you
     * got the right username and password when using other functions.
     * Returns true on successfull login - returns the error message on false.
     *
     * @return boolean/string    Returns the error message on failure - otherwise boolean true.
     */
    public function Login() {
        $param = array();
        $param['cellphone'] = $this->cellphone;
        $param['password'] = $this->password;
        $result = $this->doSOAPCall('Login',$param);
        if($result->LoginResult === '') {
            return true;
        }
        return $result->LoginResult;
    }
    
    
    /**
     * Get your balanace
     * 
     * Get your current balance from BiBoB. The amount is returned as a float
     * in Danish Kroner.
     *
     * @return float    The amount on your account balance.
     */
    public function GetBalance() {
        $param = array();
        $param['cellphone'] = $this->cellphone;
        $param['password'] = $this->password;
        $result = $this->doSOAPCall('GetBalance',$param);
        if($result->GetBalanceResult===NULL) {
            return false;
        }
        return $result->GetBalanceResult;
    }
    
    /**
    * Get your contact list
    * 
    * Get the contact list from your WebSMS at bibob.dk
    * Returns an array with the contacts. Each got the keys 
    * 'firstname', 'lastname' and 'cellphone'
    * Set parameter utf8decode to true if you want the text
    * to be encoded in ISO-8859-1
    * Returns false on failure.                                
    *
    * @param    boolean    $utf8decode    Default set to true
    * @return    array
    */
    
    public function GetContacts($utf8decode=false) {
        $param = array();
        $param['cellphone'] = $this->cellphone;
        $param['password'] = $this->password;
        $result = $this->doSOAPCall('GetContacts',$param);
        if($result->GetContactsResult===NULL) {
            return false;
        }
        $contacts = array();
        foreach($result->GetContactsResult->string as $r) {
            if($utf8decode) { $r = utf8_decode($r); }
            $rx = explode(':',$r);
            $contacts[] = array('firstname'=>$rx[0],'lastname'=>$rx[1],'cellphone'=>$rx[2]);
        }
        return $contacts;
    }
    
    /**
     * Send a SMS message
     * 
     * Send one - or more SMS messages through the WSDL. Content needs to be string
     * the string will automatic be UTF-8 encoded. Just leave date empty - otherwise
     * you want to send a scheduled message.
     * The to parameter can be a string of one cellphone numer - or can be an
     * array of 2 or more numbers. If it is an array and identical message will be
     * sent to all numbers.
     * Returns true on success and false on failure.            
     *
     * @param    string/array    $to
     * @param    string            $content
     * @param    dateTime        $date    
     * @return    boolean            True on success, False on failure
     */
    
    public function SendSms($to,$content='',$date='') {
        if(trim($content) == '') { return false; } //We need some content in the SMS
        $param = array();
        if(is_array($to)) {
            foreach($to as $number) {
                $param['smsTo']['string'][] = $number;
            }
        }
        else {
            $param['smsTo'] = array('string'=>$to);
        }
        $param['cellphone'] = $this->cellphone;
        $param['password'] = $this->password;
        $param['smscontents'] = utf8_encode($content);
        if($date=='') { $date = date("Y-m-d"); }
        $param['sendDate'] = $date;
	$param['deliveryReport'] = false; 
        $result = $this->doSOAPCall('SendMessage',$param);
        if($result===NULL) {
            return false;
        }
        return true;
    }
    
    
    /**
     * Do a SOAP call
     * 
     * Do a SOAP call to the the given WSDL. Returns the result.
     *
     * @param    string    $method    The method that have to be called
     * @param    array    $param    Parameters given as a array
     * @return    array    Array of the result
     * @access    private
     */ 
    private function doSOAPCall($method,$param=array()) {
        $soapClient = new soapClient($this->wsdl);
        $result = $soapClient->__soapCall($method,array('parameters'=>$param));
        return $result;
    }
}
?>
